package com.example.blogapi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blogapi.domain.MsTag;
import com.example.blogapi.mapper.MsTagMapper;
import com.example.blogapi.service.MsTagService;
import com.example.blogapi.vo.Result;
import com.example.blogapi.vo.TagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 13723
* @description 针对表【ms_tag】的数据库操作Service实现
* @createDate 2022-10-20 22:20:01
*/
@Service
public class MsTagServiceImpl extends ServiceImpl<MsTagMapper, MsTag>
implements MsTagService {
    @Autowired
    MsTagMapper msTagMapper;
    public TagVo copy(MsTag msTag){
        TagVo tagVo = new TagVo();
        BeanUtils.copyProperties(msTag,tagVo);
        return tagVo;
    }
    public List<TagVo> copyList(List<MsTag> tagList){
        List<TagVo> tagVoList = new ArrayList<>();
        for (MsTag tag : tagList) {
            tagVoList.add(copy(tag));
        }
        return tagVoList;
    }

    @Override
    public List<MsTag> getTagsByArticleId(long articleId) {
        List<MsTag> tags = msTagMapper.getTagsByArticleId(articleId);
        return tags;
    }

    @Override
    public List<MsTag> getHotTags(int limit) {
        List<MsTag> hotTags = msTagMapper.getHotTags(limit);
        return hotTags;
    }

    @Override
    public Result getAllTags() {
        List<MsTag> msTags = msTagMapper.selectList(null);
        return Result.success(copyList(msTags));
    }
}
